package com.web.servlet.dao.impl;

import com.web.servlet.bean.GamePlayerInfo;
import com.web.servlet.bean.RedisKey;
import com.web.servlet.bean.UserInfo;
import com.web.servlet.dao.IGamePlayerInfoDao;
import com.web.servlet.dao.UserInfoDao;
import com.web.servlet.db.DbParameter;
import com.web.servlet.db.StrategyBaseDao;
import com.web.servlet.manager.RedisManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class GamePlayerInfoDaoImpl extends StrategyBaseDao implements IGamePlayerInfoDao {
	public static final Logger log = LoggerFactory.getLogger(GamePlayerInfoDaoImpl.class);
	@Override
	public GamePlayerInfo addGamePlayerInfo(GamePlayerInfo userInfo) {
		//userInfo.setUser_id(getUserId());
		Random random=new Random();//定义随机类intresult=random.nextInt(10);
        int userid ;
        do{
            userid =  getRandom(100000, 999999);
        }while(null != RedisManager.getRedis().get(RedisKey.USERINFO+userid));

		userInfo.setPlayer_id(String.valueOf(userid));
		userInfo.setPlayer_index(userid);
		addUser(userInfo);
		return userInfo;
	}
    public static int  getRandom(int min, int max){
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return s;

    }
	public int getUserId(){
		String sqlText = "SELECT COUNT(*) as num from g_user where user_id=?;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		Random random=new Random();//定义随机类intresult=random.nextInt(10);
		int userid = random.nextInt(99999999);
		int num = 1; 
		para.put(1, new DbParameter(Types.INTEGER, userid));
		PreparedStatement pstmt = execQuery(sqlText.toString(), para);
		ResultSet rs = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					num = rs.getInt("num");
				}
			} catch (Exception e) {
				log.error("获取userid 是否使用", e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		if(0 == num){
			return userid;
		}else{
			return getUserId();
		}
	}
	public boolean addUser(GamePlayerInfo info) {
		boolean result = false;
		String sqlString = "  Insert Into t_player(" +
				"PLAYER_ID,PLAYER_INDEX,PLAYER_NAME,GOLD,PASSWORD,DIAMOND,SCORE,QQ_OPEN_ID,WX_OPEN_ID,PLAYER_TYPE,PHONE_NUMBER,ACCOUNT,REGISTIME,NICK_NAME,HEAD_IMG,PLAYER_SEX) " +
				" VALUE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.VARCHAR, info.getPlayer_id()));
		para.put(2, new DbParameter(Types.INTEGER, info.getPlayer_index()));
		para.put(3, new DbParameter(Types.VARCHAR, info.getPlayer_name()));
		para.put(4, new DbParameter(Types.INTEGER, info.getGold()));
		para.put(5, new DbParameter(Types.VARCHAR, info.getPassWord()));
		para.put(6, new DbParameter(Types.INTEGER, info.getDiamond()));
		para.put(7, new DbParameter(Types.INTEGER, info.getScore()));
		para.put(8, new DbParameter(Types.VARCHAR, info.getQq_open_id()));
		para.put(9, new DbParameter(Types.VARCHAR, info.getWx_open_id()));
		para.put(10, new DbParameter(Types.INTEGER, info.getPlayer_type()));
		para.put(11, new DbParameter(Types.INTEGER, info.getPhone_number()));
		para.put(12, new DbParameter(Types.VARCHAR, info.getAccount()));
		para.put(13, new DbParameter(Types.DATE, new Date()));
        para.put(14, new DbParameter(Types.VARCHAR, info.getPlayer_name()));
        para.put(15, new DbParameter(Types.VARCHAR, info.getHead_url()));
        para.put(16, new DbParameter(Types.INTEGER, info.getSex()));
		result = execNoneQuery(sqlString, para) > -1 ? true : false;
		return result;
	}
	
	
	
	@Override
	public GamePlayerInfo getGamePlayerInfoByName(String account, String passworld) {
		String sqlText = "select * from t_player where ACCOUNT=? and PASSWORD=?;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.VARCHAR, account));
		para.put(2, new DbParameter(Types.VARCHAR, passworld));
		return getUserInfo(sqlText, para);
	}
	@Override
	public GamePlayerInfo getGamePlayerInfoByAccount(String account) {
		String sqlText = "select * from t_player where ACCOUNT=? ;";
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(1, new DbParameter(Types.VARCHAR, account));
		return getUserInfo(sqlText, para);
	}
	private GamePlayerInfo getUserInfo(String sqlText, Map<Integer, DbParameter> para) {
		PreparedStatement pstmt = execQuery(sqlText, para);
		ResultSet rs = null;
		GamePlayerInfo info = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if (rs.last()) {
					info = new GamePlayerInfo();
					info.setPlayer_id(rs.getString("PLAYER_ID"));
					info.setPlayer_index(rs.getInt("PLAYER_INDEX"));
					info.setPlayer_name(rs.getString("PLAYER_NAME"));
					info.setGold(rs.getInt("GOLD"));
					info.setPassWord(rs.getString("PASSWORD"));
					info.setDiamond(rs.getInt("DIAMOND"));
					info.setScore(rs.getInt("SCORE"));
					info.setQq_open_id(rs.getString("QQ_OPEN_ID"));
					info.setWx_open_id(rs.getString("WX_OPEN_ID"));
					info.setPlayer_type(rs.getInt("PLAYER_TYPE"));
					info.setPhone_number(rs.getInt("PHONE_NUMBER"));
					info.setAccount(rs.getString("ACCOUNT"));
				}
			} catch (SQLException e) {
				info = null;
				log.error("执行出错" + sqlText, e);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return info;
	}

}
